package colorChanger;

import java.awt.Image;
import java.io.FileNotFoundException;
import java.io.IOException;

public class ColoredQRCode {
	private QRCode primaryQRCode;
	private QRCode secundaryQRCode;
	private QRCode coloredQRCode;

	public ColoredQRCode(String primaryQRCodeImagePath,
			String secundaryQRCodeImagePath, String format)
			throws FileNotFoundException, IOException {
		setPrimaryQRCode(new QRCode(primaryQRCodeImagePath));
		setSecundaryQRCode(new QRCode(secundaryQRCodeImagePath));
		setColoredQRCode(new QRCode("Images/BaseImages/base." + format));
		paintColoredQRCode();

	}

	public ColoredQRCode(String coloredQRCodeImagePath, String format)
			throws FileNotFoundException, IOException {

		setColoredQRCode(new QRCode(coloredQRCodeImagePath + "." + format));
		setPrimaryQRCode(new QRCode(coloredQRCodeImagePath + "." + format));
		setSecundaryQRCode(new QRCode(coloredQRCodeImagePath + "." + format));
		paintPrimaryAndSecundaryQRCodes();
		// paintDownNeightborhood(getPrimaryQRCode());
		// getSecundaryQRCode();
		// paintColoredQRCode();

	}

	public ColoredQRCode(Image image)
			throws FileNotFoundException, IOException {

		setColoredQRCode(new QRCode(image));
		setPrimaryQRCode(new QRCode(image));
		setSecundaryQRCode(new QRCode(image));
		paintPrimaryAndSecundaryQRCodes();
		// paintDownNeightborhood(getPrimaryQRCode());
		// getSecundaryQRCode();
		// paintColoredQRCode();

	}

	public void paintPrimaryAndSecundaryQRCodes() {
		int pixel;
		for (int pixelIndex = 0; pixelIndex < getColoredQRCode().getPixels().length; pixelIndex++) {

			pixel = getColoredQRCode().getPixels()[pixelIndex];

			if (RGBtoCMYK.isCyan(pixel)) {
				paintOnWhiteQRCodePixel(getPrimaryQRCode(), pixelIndex);
				paintOnBlackQRCodePixel(getSecundaryQRCode(), pixelIndex);
			} else if (RGBtoCMYK.isMagenta(pixel)) {
				paintOnBlackQRCodePixel(getPrimaryQRCode(), pixelIndex);
				paintOnWhiteQRCodePixel(getSecundaryQRCode(), pixelIndex);
			} else if (RGBtoCMYK.isWhite(pixel)) {
				paintOnWhiteQRCodePixel(getPrimaryQRCode(), pixelIndex);
				paintOnWhiteQRCodePixel(getSecundaryQRCode(), pixelIndex);
			} else {
				paintOnBlackQRCodePixel(getPrimaryQRCode(), pixelIndex);
				paintOnBlackQRCodePixel(getSecundaryQRCode(), pixelIndex);
			}
		}
	}

	private void paintOnWhiteQRCodePixel(QRCode qrcode, int pixelIndex) {
		qrcode.getPixels()[pixelIndex] = RGBtoCMYK.WHITE_PIXEL;
	}

	private void paintOnBlackQRCodePixel(QRCode qrcode, int i) {
		qrcode.getPixels()[i] = RGBtoCMYK.KEY_PIXEL;
	}

	private boolean isPrimaryWhiteAndSecundaryBlack(int primaryPixel,
			int secundaryPixel) {
		return isPixelWhite(primaryPixel) && isPixelBlack(secundaryPixel);
	}

	private boolean isPrimaryBlackAndSecundaryWhite(int primaryPixel,
			int secundaryPixel) {
		return isPixelBlack(primaryPixel) && isPixelWhite(secundaryPixel);
	}

	private void paintColoredQRCode() {
		int primaryPixel = 0;
		int secundaryPixel = 0;
		for (int i = 0; i < coloredQRCode.getPixels().length; i++) {
			primaryPixel = getPrimaryQRCode().getPixels()[i];
			secundaryPixel = getSecundaryQRCode().getPixels()[i];

			if (twoPixelAreBlacks(primaryPixel, secundaryPixel)) {
				getColoredQRCode().getPixels()[i] = RGBtoCMYK.KEY_PIXEL;
			} else if (twoPixelsAreWhite(primaryPixel, secundaryPixel)) {
				getColoredQRCode().getPixels()[i] = RGBtoCMYK.WHITE_PIXEL;
				// getColoredQRCode().getPixels()[i] = RGBtoCMYK.YELLOW_PIXEL;

			} else if (isPrimaryBlackAndSecundaryWhite(primaryPixel,
					secundaryPixel)) {
				getColoredQRCode().getPixels()[i] = RGBtoCMYK.CYAN_PIXEL;
			} else if (isPrimaryWhiteAndSecundaryBlack(primaryPixel,
					secundaryPixel)) {
				getColoredQRCode().getPixels()[i] = RGBtoCMYK.MAGENTA_PIXEL;
			}
		}
	}

	private boolean twoPixelsAreWhite(int primaryPixel, int secundaryPixel) {
		return isPixelWhite(primaryPixel) && isPixelWhite(secundaryPixel);
	}

	private boolean twoPixelAreBlacks(int primaryPixel, int secundaryPixel) {
		return isPixelBlack(primaryPixel) && isPixelBlack(secundaryPixel);
	}

	public boolean isPixelMagenta(int pixel) {
		if (pixel == RGBtoCMYK.MAGENTA_PIXEL) {
			return true;
		} else
			return false;
	}

	public boolean isPixelYellow(int pixel) {
		if (pixel == RGBtoCMYK.YELLOW_PIXEL) {
			return true;
		} else
			return false;
	}

	public boolean isPixelCyan(int pixel) {
		if (pixel == RGBtoCMYK.CYAN_PIXEL) {
			return true;
		} else
			return false;
	}

	public boolean isPixelBlack(int pixel) {

		if (pixel == RGBtoCMYK.KEY_PIXEL) {
			return true;
		} else
			return false;
	}

	public boolean isPixelWhite(int pixel) {
		if (pixel == RGBtoCMYK.WHITE_PIXEL) {
			return true;
		} else
			return false;
	}

	public QRCode getPrimaryQRCode() {
		return primaryQRCode;
	}

	public void setPrimaryQRCode(QRCode primaryQRCode) {
		this.primaryQRCode = primaryQRCode;
	}

	public QRCode getSecundaryQRCode() {
		return secundaryQRCode;
	}

	public void setSecundaryQRCode(QRCode secundaryQRCode) {
		this.secundaryQRCode = secundaryQRCode;
	}

	public QRCode getColoredQRCode() {
		if (coloredQRCode == null) {
			try {
				coloredQRCode = new QRCode("white.jpg");
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return coloredQRCode;
	}

	public void setColoredQRCode(QRCode coloredQRCode) {
		this.coloredQRCode = coloredQRCode;
	}

}
